# Replication Archive for: 
# Coppock, Alexander and Donald P. Green. 2020. 
# "Do Belief Systems Exhibit Dynamic Constraint?" 
# The Journal of Politics, Forthcoming.

library(tidyverse)
library(estimatr)
library(coefplot)

source("programs/helpers/helper_functions.R")
study_1_mturk <- read_rds("data/clean/study_1_mturk_cleaned.rds")
study_1_mturk <- study_1_mturk %>% filter(Z != "climate")

dv_df <- read_rds("data/raw/dv_df.rds")
dv_df_mturk <- filter(dv_df, dv_cat != "climate")


# regressions -------------------------------------------------------------

all_fits_mturk <-
  dv_df_mturk$dvs %>%
  map( ~ formula(paste0(., "_w3 ~ Z"))) %>%
  map( ~ lm_robust(., data = study_1_mturk)) %>%
  map(tidy) %>%
  bind_rows 

# prepare for plotting ----------------------------------------------------

all_fits <-
  bind_rows(`MTurk Sample` = all_fits_mturk,
            .id = "Study") %>%
  mutate(dvs = gsub(outcome, pattern = "_w3", replacement = ""))

gg_df <-
  all_fits %>%
  filter(term != "(Intercept)") %>%
  left_join(dv_df) %>%
  mutate(treatment = factor(term,
                            levels = c("Zpaul", "Zwallstreet", "Zveterans", "Zamtrak"),
                            labels = c("Treatment: Flat Tax", "Treatment: Wall Street", "Treatment: Veterans", "Treatment: Amtrak")),
         dv_cat_label = factor(dv_cat,
                               levels = c("flat", "wall", "vets","amtrak"),
                               labels = c("Outcomes: Flat Tax", "Outcomes: Wall Street", "Outcomes: Veterans", "Outcomes: Amtrak")),
         dv_labels_rev = factor(dv_labels, levels = rev(levels(dv_labels))))


highlight_df <-
  tibble(
    treatment = c(
      "Treatment: Wall Street",
      "Treatment: Amtrak",
      "Treatment: Veterans",
      "Treatment: Flat Tax"
    ),
    dv_cat_label = c(
      "Outcomes: Wall Street",
      "Outcomes: Amtrak",
      "Outcomes: Veterans",
      "Outcomes: Flat Tax"
    )
  )

g <-
  ggplot(gg_df,
         aes(
           x = estimate,
           y = dv_labels_rev,
           group = Study,
           color = Study,
           shape = Study
         )) +
  geom_rect(
    data = highlight_df,
    aes(
      xmin = -Inf,
      xmax = Inf,
      ymin = -Inf,
      ymax = Inf,
      x = NULL,
      y = NULL,
      group = NULL,
      color = NULL,
      shape = NULL
    ),
    fill = 'grey',
    alpha = 0.4,
    show.legend = FALSE
  ) +
  geom_point(position = position_dodgev(height = .5)) +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high),
                 position = position_dodgev(height = .5),
                 height = 0) +
  scale_color_manual(values = bpr_colors[1]) +
  scale_x_continuous(breaks = c(0, 0.33, 0.67)) +
  facet_grid(dv_cat_label ~ treatment, scales = "free_y") +
  coord_cartesian(xlim = c(-.2, .8)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  theme_bw() +
  theme(
    axis.title = element_blank(),
    legend.position = "bottom",
    strip.background = element_blank(),
    legend.key.width = unit(4, "lines"),
    text = element_text(size = 8)
  )
g

ggsave("../Drafts/figures/study_1_wave_3.pdf", g, height = 5, width = 6.5)


